package com.service;

import com.mapper.MsgMapper;
import com.util.TransactionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class MsgService {

    @Autowired
    MsgMapper mapper;

    @Autowired
    TransactionUtil transactionUtil;

//    @Transactional
//    public void insert(int j){
//        System.out.println("方法执行中");
//        mapper.insert();
//        int i = j / 0;
//    }
//    public void insert(int j){
//        TransactionStatus transactionStatus = transactionUtil.begin();
//        try{
//            System.out.println("方法执行中");
//            mapper.insert();
//            int i = j / 0;
//            transactionUtil.commit(transactionStatus);
//        }catch (Exception e){
//            transactionUtil.rollback(transactionStatus);
//        }
//    }
//    public void insert(int j){
//        System.out.println("方法执行中");
//        mapper.insert();
//        int i = j / 0;
//    }
    public void insert(int j){
        try{
            System.out.println("方法执行中");
            mapper.insert();
            int i = j / 0;
        }catch (Exception e){
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
